﻿namespace com.stifel.BranchMgmtDao
{
    using System.Collections.Generic;
    using System.Linq;

    using Microsoft.SharePoint.Linq;

    using Context = BranchMgmtDataContext;

    using CustomObject = StifelPhotoType;

    public class StifelPhotoTypeDao : StifelBaseDao
    {
        #region Fields

        public const string ListName = "stifelphototypes";

        #endregion Fields

        #region Constructors

        public StifelPhotoTypeDao( )
            : base(ListName)
        {
            CurrentWebUrl = ConfiguredWebUrl;
        }

        public StifelPhotoTypeDao( string currentWebUrl )
            : base(ListName)
        {
            this.CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public void Add( params string[ ] values )
        {
            List<CustomObject> l = new List<CustomObject>( );
            values.ToList( ).ForEach( a =>
            {
                if ( !string.IsNullOrEmpty( a ) )
                {
                    CustomObject o = new CustomObject
                    {
                        Title = a
                    };
                    l.Add( o );
                }
            } );
            if ( l.Count > 0 )
                Add( l.ToArray( ) );
        }

        public void Add( string value )
        {
            CustomObject o = new CustomObject
            {
                Title = value
            };
            Add( o );
        }

        public void Add(params CustomObject[] values )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                l.InsertAllOnSubmit( values );
                c.SubmitChanges( );
            }
        }

        public void Add( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                l.InsertOnSubmit( value );
                c.SubmitChanges( );
            }
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public List<CustomObject> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<CustomObject> l = c.GetList<CustomObject>( ListTitle ).ToList( );
                return l;
            }
        }

        #endregion Methods
    }
}